package com.eleaf.adbb.core;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eleaf.adbb.template.ClassTemplate;
import com.eleaf.adbb.template.android.CodeTemplate;
import com.eleaf.adbb.util.BaseUtil;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * 布局代码生成类
 * 
 * @author Eleaf
 * 
 */
public class CodeGenerater {

	Configuration freeMarkerCfg = new Configuration ();
	Template template = null;

	public CodeGenerater() {
		freeMarkerCfg.setClassForTemplateLoading ( getClass (), "ftl" );
		freeMarkerCfg.setObjectWrapper ( new DefaultObjectWrapper () );
		try {
			template = freeMarkerCfg.getTemplate ( "javacode.ftl" );
		} catch ( IOException e ) {
			e.printStackTrace ();
		}
	}

	/**
	 * 生成布局文件对应的JAVA代码
	 * 
	 * @param projectPath android项目路径
	 * @param activityTemplate activity模版
	 * @param fragmentTemplate fragment模版
	 */
	public void generate( String projectPath, ClassTemplate activityTemplate, ClassTemplate fragmentTemplate ) {
		String layoutPath = BaseUtil.appendString ( projectPath, "\\res\\layout\\" );
		String codePath = BaseUtil.appendString ( projectPath, "\\src\\" );
		generate ( layoutPath, codePath, activityTemplate, fragmentTemplate );
	}

	/**
	 * 生成布局文件对应的JAVA代码
	 * 
	 * @param layoutPath 布局文件路径
	 * @param codePath 代码路径
	 * @param activityTemplate activity模版
	 * @param fragmentTemplate fragment模版
	 */
	public void generate( String layoutPath, String codePath, ClassTemplate activityTemplate, ClassTemplate fragmentTemplate ) {
		LayoutReader layoutReader = new LayoutReader ();
		try {
			List<CodeTemplate> read = layoutReader.read ( layoutPath, activityTemplate, fragmentTemplate );
			for ( CodeTemplate cfg : read ) {
				generateRequest ( codePath, cfg );
			}
			System.out.println ( "完成" );
		} catch ( Exception e ) {
			e.printStackTrace ();
		}
	}

	/**
	 * 读取freemaker模版生成对应JAVA文件
	 * 
	 * @param codePath 代码路径
	 * @param obj 模版
	 */
	private void generateRequest( String codePath, CodeTemplate obj ) {
		String hostPath = obj.getHostPackage ().replace ( ".", "/" );
		String lastPath = obj.getLastPackage ().replace ( ".", "/" );
		String reqFileName = BaseUtil.appendString ( codePath, "/", hostPath, "/", lastPath, "/" );
		File file = new File ( reqFileName );
		if ( !file.exists () )
			file.mkdirs ();
		reqFileName = BaseUtil.appendString ( reqFileName, obj.getClassName (), ".java" );
		try {
			Map<String, Object> parameters = new HashMap<String, Object> ();
			parameters.put ( "cfg", obj );
			OutputStreamWriter writer = new OutputStreamWriter ( new FileOutputStream ( reqFileName ), "UTF-8" );
			template.process ( parameters, writer );
			writer.flush ();
		} catch ( IOException e ) {
			e.printStackTrace ();
		} catch ( TemplateException e ) {
			e.printStackTrace ();
		}

	}

}
